import service from '@/utils/request'
import axios from 'axios'
function Post(url, params, cancelName) {
    return new Promise((resolve, reject) => {
        if (cancelName) {
            const cancelToken = new axios.CancelToken(function executor(canceler) {
                window.cancelApi[cancelName] = canceler
            })
            params.cancelToken = cancelToken
        }
        service.post(url, params)
            .then(response => {
                resolve(response)
            }, err => {
                reject(err)
            })
            .catch((error) => {
                reject(error)
            })
    })
}
function Get(url, param, cancelName) {
    return new Promise((resolve, reject) => {
        var cancelToken
        if (cancelName) {
            cancelToken = new axios.CancelToken(function executor(canceler) {
                window.cancelApi[cancelName] = canceler
            })
        }
        service.get(url, {
            params: param,
            cancelToken: cancelToken
        })
            .then(response => {
                resolve(response)
            }, err => {
                reject(err)
            })
            .catch((error) => {
                reject(error)
            })
    })
}
function GetFile(url, param) {
    return new Promise((resolve, reject) => {
        return service({
            method: 'get',
            url: url,
            params: param,
            responseType: 'blob'

        })
            .then(response => {
                resolve(response)
            }, err => {
                reject(err)
            })
            .catch((error) => {
                reject(error)
            })
    })
}
function Put(url, params) {
    return new Promise((resolve, reject) => {
        service.put(url, params)
            .then(response => {
                resolve(response)
            }, err => {
                reject(err)
            })
            .catch((error) => {
                reject(error)
            })
    })
}
function Delete(url, params) {
    return new Promise((resolve, reject) => {
        service.delete(url, {
            data: params
        })
            .then(response => {
                resolve(response)
            }, err => {
                reject(err)
            })
            .catch((error) => {
                reject(error)
            })
    })
}
export default {
    Post,
    Get,
    GetFile,
    Delete,
    Put
}
